#pragma strict
@script ExecuteInEditMode
//////////////////////////////////////////////////////
///序列帧 动画
//////////////////////////////////////////////////////
var xCount:int = 1;
var yCount:int = 1;
var speed:float = 5;
var isInvertY:boolean;

var texName:String = "_MainTex";

private var x:int;
private var y:int;

private var tileFactor:Vector2;
private var lastTime:float;
private var intervalTime:float;

function Start () {
	intervalTime = 1.0/speed;
	SetupScale();
}

function Update () {
	if(renderer){
		SetupOffset();
	}
}

function SetupScale(){
	if(renderer){
		tileFactor = Vector2(1.0/xCount,1.0/yCount);
		renderer.material.SetTextureScale(texName,tileFactor);
	}
}

function SetupOffset(){
	if(Time.time - lastTime > intervalTime){
		lastTime = Time.time;
		x ++;
		if(x >= xCount){
			x = 0;
			y ++;
			if(y >= yCount)
				y = 0;
		}
		var newY:float = y;
		if(isInvertY)
			newY = yCount - y - 1;
		renderer.material.SetTextureOffset(texName,Vector2(tileFactor.x * x,tileFactor.y * newY) );
	}
}